package com.xinruke.hostels.manage.user.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xinruke.hostels.common.cache.ManageUserCache;
import com.xinruke.hostels.common.po.XtJuese;
import com.xinruke.hostels.common.po.XtJuesecaidanGuanxi;
import com.xinruke.hostels.common.po.XtYewuyonghu;
import com.xinruke.hostels.common.cache.vo.LoginManageResultVO;
import com.xinruke.hostels.common.vo.query.RowsDataVO;
import com.xinruke.hostels.manage.user.interfaces.dto.*;
import com.xinruke.hostels.manage.user.mapper.XtCaidanAnniuMapper;
import com.xinruke.hostels.manage.user.mapper.XtCaidanMapper;
import com.xinruke.hostels.manage.user.mapper.XtJueseMapper;
import com.xinruke.hostels.manage.user.mapper.XtJuesecaidanGuanxiMapper;
import com.xinruke.hostels.manage.user.service.RightService;
import com.xinruke.hostels.manage.user.service.RoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.util.Sqls;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author GuoZheng
 * @ClassName RoleServiceImpl
 * @date：2019/6/14
 * @version: V1.0.0
 * @description：
 */
@Service
public class RoleServiceImpl implements RoleService {
    // todo 获取当前用户id
    private int currentUserId = 0;

    @Resource
    private XtJueseMapper xtJueseMapper;

    @Resource
    private XtJuesecaidanGuanxiMapper xtJuesecaidanGuanxiMapper;
    @Resource
    private ManageUserCache manageUserCache;

    @Resource
    private XtCaidanMapper xtCaidanMapper;

    @Resource
    private XtCaidanAnniuMapper xtCaidanAnniuMapper;

    @Resource
    private RightService rightService;


    @Override
    @Transactional
    public int add(RoleAddDTO roleAddDTO) {

        XtJuese role = new XtJuese();
        //复制所属系统，名称和描述
        BeanUtils.copyProperties(roleAddDTO, role);
        role.setChuangjianrenId(currentUserId);
        role.setChuangjianshijian(new Date());
        role.setQiyongzhuangtai(XtJuese.QIYONGZHUANGTAI_YOU_XIOA);
        role.setShanchubiaoshi(XtJuese.SHANCHUBIAOSHI_WU_XIAO);
        role.setJiudianId(roleAddDTO.getJiudianId());
        return xtJueseMapper.insert(role);
    }

    @Override
    @Transactional
    public int modify(RoleModifyDTO roleModifyDTO) {
        XtJuese role = new XtJuese();
        //复制所属系统，名称和描述
        BeanUtils.copyProperties(roleModifyDTO, role);
        role.setGengxinrenId(currentUserId);
        role.setGengxinshijian(new Date());
        role.setQiyongzhuangtai(XtJuese.QIYONGZHUANGTAI_YOU_XIOA);
        role.setShanchubiaoshi(XtJuese.SHANCHUBIAOSHI_WU_XIAO);
        return xtJueseMapper.updateByPrimaryKeySelective(role);

    }

    @Override
    @Transactional
    public int delete(RoleDeleteDTO roleDeleteDTO) {
        XtJuese role = new XtJuese();
        role.setId(roleDeleteDTO.getId());
        role.setGengxinrenId(currentUserId);
        role.setGengxinshijian(new Date());
        role.setQiyongzhuangtai(XtJuese.QIYONGZHUANGTAI_WU_XIOA);
        role.setShanchubiaoshi(XtJuese.SHANCHUBIAOSHI_YOU_XIAO);
        return xtJueseMapper.updateByPrimaryKeySelective(role);
    }

    /**
     * 查询角色列表
     * @param roleQueryDTO
     * @return
     */
    @Override
    public RowsDataVO<RoleQueryResultDTO> getRoleList(RoleQueryDTO roleQueryDTO) {
        RowsDataVO<RoleQueryResultDTO> rowsDataVO = new RowsDataVO<>();

        LoginManageResultVO currentUser = manageUserCache.getLoginManageUserInfo();
        //如果用户不是超管
        if (!currentUser.getYonghuleixing().equals(XtYewuyonghu.YONGHULEIXNG_CHANOGUAN)) {
            roleQueryDTO.setUserId(currentUser.getId());

        }
        Page<XtJuese> page = PageHelper.startPage(roleQueryDTO.getPageNo(), roleQueryDTO.getPageSize());
        //查询关系表
        List<RoleQueryResultDTO> roleQueryResultDTOS = xtJueseMapper.selectRolesAndUsageQuantity(roleQueryDTO);
        List<RoleQueryResultDTO> dtos = roleQueryResultDTOS;
        rowsDataVO.setPageNo(page.getPageNum());
        rowsDataVO.setTotalPages(page.getPages());
        rowsDataVO.setTotalRecords(page.getTotal());
        rowsDataVO.setRows(dtos);
        return rowsDataVO;
    }


    @Override
    public RoleDetailResultDTO detail(RoleDetailQueryDTO roleDeleteDTO) {
        XtJuese xtJuese = xtJueseMapper.selectByPrimaryKey(roleDeleteDTO.getId());
        if (xtJuese == null) {
            return null;
        }
        RoleDetailResultDTO dto = new RoleDetailResultDTO();
        BeanUtils.copyProperties(xtJuese, dto);
        return dto;

    }

    /**
     * 通过角色id集合拿到角色菜单对应关系
     *
     * @param roleIds
     * @return
     */
    @Override
    public List<XtJuesecaidanGuanxi> getRightRelationsByRoleIds(List<Integer> roleIds) {
        List<XtJuesecaidanGuanxi> relations = xtJuesecaidanGuanxiMapper.selectByExample(Example.builder(XtJuesecaidanGuanxi.class)
                .where(Sqls.custom()
                        .andEqualTo("shanchubiaoshi", XtJuesecaidanGuanxi.SHANCHUBIAOSHI_WU_XIOA)
                        .andIn("jueseId", roleIds)
                ).build());

        return relations;
    }


}
